package com.project.service.impl;

import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserService implements IUserService {
    @Autowired
    private IUserMapper userMapper;

    /**
     * 根据id查找旧pwd
     * @param userId 用户id
     */
    @Override
    public String findOldPwd(Integer userId) {
        return userMapper.findOldPwd(userId);
    }

    /**
     * 修改密码
     *
     * @param userId 用户id
     * @param pwd1   旧密码
     * @param pwd2   新密码
     * @param pwd3   确认的新密码
     * @return
     */
    @Override
    public String updatePwdById(Integer userId, String pwd1, String pwd2, String pwd3) {
        if(!userMapper.findOldPwd(userId).equals(pwd1)){
            return "旧密码输入错误";
        }
        if (pwd1.equals(pwd2)) {
            return "新密码不能与旧密码一致";
        }
        if (!pwd2.equals(pwd3)) {
            return "确认的新密码与新密码不一致";
        }
        userMapper.updatePwdById(userId, pwd1, pwd2, pwd3);
        return "修改成功";
    }
}
